//
//  LXDanmuView.m
//  LeYao
//
//  Created by LX on 15/5/5.
//  Copyright (c) 2015年 LexLee. All rights reserved.
//

#import "LXDanmuView.h"
#import "SVProgressHUD.h"
@implementation LXDanmuView
-(id)initDanMuViewWithTypeImage:(UIImage *)typeImage textBgImage:(UIImage *)textBgImage textString:(NSString *)textString isShow:(BOOL)isShow{
    CGRect bounds = [UIScreen mainScreen].bounds;
    if (self == [super initWithFrame:CGRectMake(0, 0, CGRectGetWidth(bounds), 30)]) {
        [self addDanMuViewWithTypeImage:typeImage textBgImage:textBgImage textString:textString isShow:isShow];
    }
    return self;
}
-(id)initDanMuViewWithTypeImage:(UIImage *)typeImage textBgImage:(UIImage *)textBgImage textString:(NSString *)textString 
{
    CGRect bounds = [UIScreen mainScreen].bounds;
    if (self == [super initWithFrame:CGRectMake(0, 0, CGRectGetWidth(bounds), 30)]) {
        [self addDanMuViewWithTypeImage:typeImage textBgImage:textBgImage textString:textString isShow:NO];
    }
    return self;
}

-(void)addDanMuViewWithTypeImage:(UIImage *)typeImage textBgImage:(UIImage *)textBgImage textString:(NSString *)textString isShow:(BOOL)isShow{
    self.backgroundColor = [UIColor clearColor];
    _animationLabel = [self addDanWithtText:textString];
    _animationLabel.frame = CGRectMake(6, 5, CGRectGetWidth(_animationLabel.bounds), 20);
    [self addSubview:_animationLabel];
    
    _DimageView = [[UIImageView alloc] init];
    if (typeImage) {
        [_DimageView setImage:typeImage];
    }
    if (!typeImage) {
        _DimageView.backgroundColor = [UIColor whiteColor];
    }
    _DimageView.frame = CGRectMake(5, 12, 6, 6);
    
    //        if ((CGRectGetMaxX(lab.frame) > [UIScreen mainScreen].bounds.size.width) && 1) {
    //            _DimageView.frame = CGRectMake(CGRectGetMaxX(lab.frame) + 9, 11, 6, 6);
    //        }
    
    _DimageView.layer.cornerRadius = CGRectGetHeight(_DimageView.bounds)/2;
    _DimageView.layer.masksToBounds = YES;
    _isAnimation = YES;
    [self animationLogoImage2:_DimageView];
    [self addSubview:_DimageView];
    
    
    
    //        UIImageView *bgImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 50, CGRectGetHeight(self.bounds))];
    //        [self addSubview:bgImageView];
    
    //        CGFloat  wP = (CGRectGetWidth(lab.bounds) + 20) < 85 ? 85:(CGRectGetWidth(lab.bounds) + 20);
    //        bgImageView.frame = CGRectMake(0, 0, wP + 10, CGRectGetHeight(self.bounds));
    //        [bgImageView setImage:[[UIImage imageNamed:"] resizableImageWithCapInsets:UIEdgeInsetsMake(30, 30, 30, 30)]];
    
    UIButton *butto = [UIButton buttonWithType:UIButtonTypeCustom];
    [butto setFrame:CGRectMake(0, 0, 12, 12)];
    butto.center = CGPointMake(_DimageView.center.x, _DimageView.center.y);
    [butto setImage:textBgImage forState:0];
    if (!isShow) {
        [butto addTarget:self action:@selector(closeBtn) forControlEvents:UIControlEventTouchUpInside];
    }
    [self addSubview:butto];
    self.frame = CGRectMake(0, 0, CGRectGetMaxX(_animationLabel.frame), 30);
    if (!isShow) {
        [self addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(closeBtn)]];
    }else {
        [self addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(closeBtnSVP)]];
    }

}
-(FKAnimationLabel *)addDanWithtText:(NSString *)text{
    CGSize sss = [LXCommonUtils calculateWithText:text size:CGSizeMake(CGRectGetWidth(self.bounds) - 25 - 40, 30) font:[UIFont systemFontOfSize:14]];
    FKAnimationLabel *view = [[FKAnimationLabel alloc]initWithFrame:CGRectMake(0, 0, sss.width + 15, sss.height)];
    view.text = [NSString stringWithFormat:@"  %@",text];
    view.textColor = [UIColor colorWithRed:0.322 green:0.769 blue:0.722 alpha:1.000];
    view.font = [UIFont systemFontOfSize:14];
    view.backgroundColor = [UIColor clearColor];
    return view;

}
-(void)closeBtn{
    [self removeFromSuperview];
    if (_timer) {
        [_timer invalidate];
        _timer = nil;
    }

}
-(void)closeBtnSVP{
    [SVProgressHUD showSuccessWithStatus:@"中国好商品，尽在“海苔君”！" duration:1.5];
}

-(void)dealloc
{
    NSLog(@"DEALLOC");

}
-(void)animationLogoImage2:(UIView*)image
{
    image.userInteractionEnabled = YES;
    _isQuit = YES;

    if (_isQuit )
    {
        
        CGAffineTransform t = image.transform;
        _isAnimation = NO;
        [UIView animateWithDuration:0.4 animations:^{
            image.transform = CGAffineTransformScale(image.transform, 1.5, 1.5);
        } completion:^(BOOL finished) {
            
            image.transform = t;
            [UIView animateWithDuration:0.3 animations:^{
                
                image.transform = CGAffineTransformScale(image.transform, 0.7, 0.7);
                
            } completion:^(BOOL finished) {
                
                image.transform = t;
                [UIView animateWithDuration:0.3 animations:^{
                    
                    image.transform = CGAffineTransformScale(image.transform, 1.5, 1.5);
                } completion:^(BOOL finished) {
                    
                    image.transform = t;
                    [UIView animateWithDuration:0.2 animations:^{
                        
                        image.transform = CGAffineTransformScale(image.transform, 0.7, 0.7);
                    } completion:^(BOOL finished) {
                        image.transform = t;
                        [self performSelector:@selector(animationLogoImage2:) withObject:image afterDelay:2];
                        _isAnimation = YES;
                        if (!_timer) {
                            _timer = [NSTimer scheduledTimerWithTimeInterval:0.6 target:self selector:@selector(addBowen) userInfo:nil repeats:YES];
                        }
                    }];
                    
                }];
                
            }];
        }];
    }
}


-(void)addBowen
{
    NSLog(@"===============闪一闪==============");
    CALayer *waveLayer=[CALayer layer];
    waveLayer.frame = CGRectMake(_DimageView.center.x-7,_DimageView.center.y-7, 14, 14);
    waveLayer.borderColor =[UIColor colorWithWhite:0.6 alpha:0.5].CGColor;
    waveLayer.borderWidth = 150;
    waveLayer.cornerRadius =7;
    [self.layer insertSublayer:waveLayer atIndex:0];
    [self scaleBegin:waveLayer];

    [self performSelector:@selector(removeLayer:) withObject:waveLayer afterDelay:1.8];
}

- (void)removeLayer:(CALayer *)layer
{
    if (layer) {
        [layer removeFromSuperlayer];
    }
}
-(void)scaleBegin:(CALayer *)aLayer
{
    _isQuit = YES;
    
    if (_isQuit )
    {
        const float maxScale=2;
        if (aLayer.transform.m11<maxScale && _isAnimation) {
            if (aLayer.transform.m11==1.0) {
                [aLayer setTransform:CATransform3DMakeScale( 1.1, 1.1, 1.0)];
                
            }else{
                [aLayer setTransform:CATransform3DScale(aLayer.transform, 1.1, 1.1, 1.0)];
            }
            [self performSelector:_cmd withObject:aLayer afterDelay:0.08];
        }
        else [aLayer removeFromSuperlayer];

    }else{
        if (_timer) {
            [_timer invalidate];
            _timer = nil;
        }
    }
}



@end
